package com.collections.excercises;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class LinkedAndArrayListComparation {

	private static List<String> lList = new LinkedList<String>();
	private static List<String> aList = new ArrayList<String>(10000);
	private static final long NUMBER_OF_WARMUP_ITERATIONS = 100_000;
	private static Random randomNumber = new Random();
	
	public static void main(String[] args) {
		warmUp();

		long linkedListResults = 0, arrayListResults = 0;
		int numberOfIterations = 0;
		
		while(  numberOfIterations<10000){
			numberOfIterations++;
			linkedListResults = addElements(lList, numberOfIterations);
			arrayListResults = addElements(aList, numberOfIterations);
			if (numberOfIterations%100==0)
				System.out.println(linkedListResults + " " + arrayListResults);
			
		}
	}
	
	private static void warmUp(){
		for (long i = 0; i < NUMBER_OF_WARMUP_ITERATIONS; i++) {
			Long longNumber = randomNumber.nextLong();
			lList.add(0,longNumber.toString());
			aList.add(0,longNumber.toString());
		}

		lList.clear();
		aList.clear();
		
	}
	
	private static long addElements(List<String> list, int numberOfIterations){
		long start,end,counter = 0;
		
		for (int i = 0; i < numberOfIterations; i++) {
			Long longNumber = randomNumber.nextLong();
			start = System.nanoTime();
			list.add(0,longNumber.toString());
			end = System.nanoTime();
			counter+=end-start;
		}
		list.clear();
		return counter/numberOfIterations;
	}
}
